<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="isshowDialog"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" type="index" width="120" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="small" type="success" @click="hjuese(scope.row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hedit(scope)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel($event,scope.row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :page-sizes="[3,5,7,10,20]"
                :current-page.sync="curPage"
                :total="total"
                :page-size="pageParams.pagesize"
                layout="sizes,prev,pager,next,jumper"
                @size-change="hsize"
                @current-change="hCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <div>
      <el-dialog
        :title="iseait? '编辑角色':' 添加角色'"
        :visible.sync="showDialog"
        width="30%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <el-form ref="form" label-width="100px" :rules="rules" :model="roleForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="roleForm.description" />
          </el-form-item>

        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="doAdd">确 定</el-button>
        </span>
      </el-dialog>

    </div>
    <el-dialog
      title="分配角色"
      :visible.sync="showDialogAssign"
      width="30%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <assignPermission :id="curid" ref="assign" @close="showDialogAssign=false" />
      <el-dialog />
    </el-dialog></div>
  <!-- 弹框 -->

</template>

<script>
import assignPermission from './assignPermission.vue'
import { getRoles, deleteRole, addRole, updateRole } from '@/api/setting.js'
export default {
  components: { assignPermission },
  data() {
    return {
      curid: '',
      showDialogAssign: false, // 分配角色默认不显示
      iseait: false,
      list: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 3 // 每页两条  --- 要与pagination中page-size一致
      },
      curPage: 0,
      total: 0, // 总的数据条数
      showDialog: false, // 控制弹框显示
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      },
      roleForm: {
        name: '',
        description: ''
      }
    }
  },
  computed: {
    // 表格中最大的页码
    maxNum() {
      return Math.ceil(this.total / this.pageParams.pagesize)
    },
    isLastPageFulled() {
      return this.total % this.pageParams.pagesize === 0
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 分配角色方法
    hjuese(id) {
      this.curid = id
      this.showDialogAssign = true
      this.$nextTick(() => {
        this.$refs.assign.loadgetPermissionDetail()
      })
    },
    // 调整每页显示的数据
    hsize(page) {
      this.pageParams.pagesize = page
      this.loadRoles()
    },
    // 关闭弹弹窗重置表单
    hClose() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.form.resetFields()
    },
    async dohedit() {
      await updateRole(this.roleForm)
    },
    // 编辑角色方法
    hedit(data) {
      // console.log(data)
      this.iseait = true
      this.showDialog = true
      this.roleForm = { ...data.row }
    },
    // 添加角色方法
    doAdd() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 做具体的添加动作
          if (this.iseait) {
            this.showDialog = false
            this.dohedit()
          } else {
            await addRole(this.roleForm)
            if (this.isLastPageFulled) {
              this.pageParams.page = this.maxNum + 1
            } else {
              this.pageParams.page = this.maxNum
            }
          }

          // if(表格的最后一页是满) {
          //   想查看最新添加的数据，要跳到最大页码的下一页
          //  }

          this.loadRoles()
        }
      })
    },
    // 显示弹框方法
    isshowDialog() {
      this.iseait = false
      this.showDialog = true
    },
    // 获取下一页的页码发数据
    hCurrentChange(curpage) {
      this.pageParams.page = curpage
      this.loadRoles()
    },
    async loadRoles() {
      const res = await getRoles(this.pageParams)
      this.total = res.data.total
      this.list = res.data.rows
      this.curPage = this.pageParams.page
    },
    // 删除功能
    async doDel(id) {
      await deleteRole(id)
      this.loadRoles()
    },
    hDel(e, id) {
      e.target.blur()
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.list.length === 1) {
          this.pageParams.page--
          if (this.pageParams.page <= 0) {
            this.pageParams.page = 1
          }
        }
        this.doDel(id)
      }).catch((err) => {
        console.log(err)
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
